//
//  NewsViewController.m
//  news
//
//  Created by imacbookair on 16/3/9.
//  Copyright © 2016年 imacbookair. All rights reserved.
//

#import "NewsViewController.h"
#import "PrefixHeader.pch"
#import "DadaViewController.h"
#import "OndDayViewController.h"




@interface NewsViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, DadaDelegate, UIScrollViewAccessibilityDelegate>
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)UIScrollView *scroll;//滑动条
@property (nonatomic, strong)NSMutableArray *buttonArr;// 按钮数组
@end

@implementation NewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 添加按钮
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"24xiaoshi"] style:UIBarButtonItemStylePlain target:self action:@selector(oneDay:)];
    
    self.navigationController.navigationBar.tintColor = [UIColor lightGrayColor];
    self.navigationItem.rightBarButtonItems = @[item1];
    self.navigationController.navigationBar.translucent = NO;
    
    [self createCollectionView];
    [self createScroll];
    
}

#pragma mark - scrollView协议方法
- (void)createCollectionView
{
    // CollectionViewFlowLayout创建
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 60);
    // 横屏开启
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // 边界预留
    layout.sectionInset = UIEdgeInsetsMake(50 * KWITH, 0, 0, 0);
    // 列间距
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    // collectionView创建
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    //代理签订
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    // 允许滚动
    _collectionView.pagingEnabled = YES;
    
    
    [_collectionView registerClass:[HomeCollectionViewCell class] forCellWithReuseIdentifier:@"HomeCollectionViewCell"];
    [_collectionView registerClass:[DadaCollectionViewCell class] forCellWithReuseIdentifier:@"DadaCollectionViewCell"];
    [_collectionView registerClass:[TiyuCollectionViewCell class] forCellWithReuseIdentifier:@"TiyuCollectionViewCell"];
    [_collectionView registerClass:[QiCheCollectionViewCell class] forCellWithReuseIdentifier:@"QiCheCollectionViewCell"];
    [_collectionView registerClass:[YuleCollectionViewCell class] forCellWithReuseIdentifier:@"YuleCollectionViewCell"];
    [self.view addSubview:_collectionView];
    [_collectionView NightWithType:UIViewColorTypeNormal];
}

#pragma mark - scrollView协议方法



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    
    if (indexPath.row == 0) {
        HomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeCollectionViewCell" forIndexPath:indexPath];

        cell.delegate = self;
        
        return cell;
    }else if (indexPath.row == 1){
        DadaCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DadaCollectionViewCell" forIndexPath:indexPath];
        cell.delegate = self;
        

        return cell;
    }else if (indexPath.row == 2){
        TiyuCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TiyuCollectionViewCell" forIndexPath:indexPath];
        cell.delegate = self;

        return cell;
    }else if (indexPath.row == 3){
        QiCheCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QiCheCollectionViewCell" forIndexPath:indexPath];
        cell.delegate = self;

        
        return cell;
    }else {
        YuleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YuleCollectionViewCell" forIndexPath:indexPath];
        cell.delegate = self;

        return cell;
    }
    
    
}

#pragma mark - 滑动条

- (void)createScroll
{
    NSArray *arr = @[@"头条", @"哒哒", @"体育", @"汽车", @"娱乐"];
    self.buttonArr = [NSMutableArray array];
    // 创建scrollView
    self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30 * KHIGHT)];
    _scroll.backgroundColor = [UIColor whiteColor];
    
    
    
    // 设置滚动视图下方的线条隐藏
    self.scroll.showsHorizontalScrollIndicator = NO;
    // 滚动视图的尺寸设置
    _scroll.contentSize = CGSizeMake(70 * KWITH * arr.count, 0);
    
    // 签订协议
    _scroll.delegate = self;
    [self.view addSubview:_scroll];
    // 添加按钮
    for (NSInteger i = 0; i < arr.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(70 * KWITH * i, 0, 70 * KWITH, 40 * KHIGHT);
        button.tag = 2016 + i;
        [button setTitle:arr[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.buttonArr addObject:button];
        [_scroll addSubview:button];
//        [button NightWithType:UIViewColorTypeNormal];
        
    }
    // 首次进入页面 第一个按钮为红色
    [_buttonArr[0] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_scroll NightWithType:UIViewColorTypeNormal];
    

}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}
// button的触发方法
- (void)buttonClick: (UIButton *)button
{
    for (UIButton *button in _buttonArr) {
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    // 选中按钮为红色
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    [_collectionView setContentOffset:CGPointMake(self.view.frame.size.width * (button.tag - 2016), 0) animated:YES];
}

#pragma mark - scrollView的协议方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger i = scrollView.contentOffset.x / self.view.frame.size.width;
    
    for (UIButton *button in _buttonArr) {
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
    // 对应的页面的按钮为红色
    [_buttonArr[i] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}

#pragma mark - 协议方法实现
- (void)DadaValue:(NSString *)url
{
    DadaViewController *da = [[DadaViewController alloc]init];
    da.str = url;
    [self.navigationController pushViewController:da animated:YES];
    
}
- (void)oneDay:(UIBarButtonItem *)oneDay
{
    OndDayViewController *one = [[OndDayViewController alloc]init];
    [self.navigationController pushViewController:one animated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
